package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.IProductTypeDao;
import com.cn.wanxi.dao.impl.ProductTypeDaoImpl;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductTypeModel;
import com.cn.wanxi.service.IProductTypeService;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductTypeServiceImpl implements IProductTypeService {

    IProductTypeDao productTypeDao = new ProductTypeDaoImpl();

    @Override
    public List<ProductTypeModel> getProductTypeList() {
        Jedis jedis = new Jedis();
        List<ProductTypeModel> list = null;
        try {
            if (jedis.exists("product_type_id")){
                list = productTypeDao.getProductTypeListByRedis();
            }else {
                list = productTypeDao.getProductTypeList();
                productTypeDao.setProductTypeInRedis(list);
            }
        }catch (Exception e){
            list = productTypeDao.getProductTypeList();
        }
        return list;

    }

    @Override
    public ProductTypeModel getProductType(int id) {
        return productTypeDao.getProductType(id);
    }

    @Override
    public int addProductType(ProductTypeModel productType) {
        return productTypeDao.addProductType(productType);
    }

    @Override
    public int deleteProductType(int id) {
        return productTypeDao.deleteProductType(id);
    }

    @Override
    public int updateProductType(ProductTypeModel productType) {
        return productTypeDao.updateProductType(productType);
    }

    @Override
    public PageModel<ProductTypeModel> getProductTypes(Map<String,Object> map) {
        PageModel pager = new PageModel();
        pager.setList(productTypeDao.getProductTypes(map));
        pager.setTotal(productTypeDao.getProductTypeCount(map));
        return pager;
    }

    @Override
    public int updateShow(int id, int show) {
        ProductTypeModel productType = new ProductTypeModel();
        productType.setId(id);
        productType.setIsShow(show);
        return productTypeDao.updateShow(productType);
    }
}
